// DlgEditUser.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgEditUser.h"
#include "DlgChangePwd.h"
#include "DlgPickUser.h"
#include "DlgConfirm.h"

// CDlgEditUser dialog

IMPLEMENT_DYNAMIC(CDlgEditUser, CDialog)

CDlgEditUser::CDlgEditUser(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEditUser::IDD, pParent)
	, m_szID(_T(""))
	, m_szUserPassword(_T(""))
	, m_szDateJoin(_T(""))
	, m_szDateLogin(_T(""))
	, m_szUserName(_T(""))
	, m_szUserEmail(_T(""))
	, m_szCompany(_T(""))
	, m_szAddress1(_T(""))
	, m_szAddress2(_T(""))
	, m_szState(_T(""))
	, m_szZipCode(_T(""))
	, m_szCountry(_T(""))
	, m_szPhone(_T(""))
	, m_szFax(_T(""))
	, m_szActivated(FALSE)
	, m_szCity(_T(""))
	, m_szAccountNumber(_T(""))
	, m_szShippingNumber(_T(""))
	, m_szNotes(_T(""))
	, m_szAltEmail1(_T(""))
	, m_szBillAddress1(_T(""))
	, m_szBillAddress2(_T(""))
	, m_szBillCity(_T(""))
	, m_szBillState(_T(""))
	, m_szBillZipcode(_T(""))
	, m_szBillCountry(_T(""))
	, m_szShipTo(_T(""))
	, m_szBillTo(_T(""))
	, m_dFeeMax(0.0)
	, m_bTaxable(FALSE)
	, m_bNotify(FALSE)
{
	m_bAddUser = FALSE;
	m_nActivationCode = 0;
	m_nUserID = -1;
	m_bChangePwd = FALSE;
}

CDlgEditUser::~CDlgEditUser()
{
}

void CDlgEditUser::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_ID, m_szID);
	DDX_Text(pDX, IDC_EDIT_DATE_JOIN, m_szDateJoin);
	DDX_Text(pDX, IDC_EDIT_DATE_LOGIN, m_szDateLogin);
	DDX_Text(pDX, IDC_EDIT_USER_NAME, m_szUserName);
	DDX_Text(pDX, IDC_EDIT_USER_EMAIL, m_szUserEmail);
	DDX_Text(pDX, IDC_EDIT_COMPANY, m_szCompany);
	DDX_Text(pDX, IDC_EDIT_ADDRESS1, m_szAddress1);
	DDX_Text(pDX, IDC_EDIT_ADDRESS2, m_szAddress2);
	DDX_Text(pDX, IDC_EDIT_STATE, m_szState);
	DDX_Text(pDX, IDC_EDIT_ZIPCODE, m_szZipCode);
	DDX_Text(pDX, IDC_EDIT_COUNTRY, m_szCountry);
	DDX_Text(pDX, IDC_EDIT_PHONE, m_szPhone);
	DDX_Text(pDX, IDC_EDIT_FAX, m_szFax);
	DDX_Check(pDX, IDC_CHECK_ACTIVATED, m_szActivated);
	DDX_Text(pDX, IDC_EDIT_CITY, m_szCity);
	DDX_Text(pDX, IDC_EDIT_ACCOUNT_NUMBER, m_szAccountNumber);
	DDX_Control(pDX, IDC_COMBO_TYPE, m_ctrlUserType);
	DDX_Text(pDX, IDC_EDIT_SHIPPING_NUMBER, m_szShippingNumber);
	DDX_Control(pDX, IDC_COMBO_REFERRALS, m_ctrlReferrals);
	DDX_Control(pDX, IDC_COMBO_METHOD, m_ctrlMethod);
	DDX_Text(pDX, IDC_EDIT_USER_NOTES, m_szNotes);
	DDX_Text(pDX, IDC_EDIT_USER_EMAIL1, m_szAltEmail1);
	DDX_Text(pDX, IDC_EDIT_BILL_ADDRESS1, m_szBillAddress1);
	DDX_Text(pDX, IDC_EDIT_BILL_ADDRESS2, m_szBillAddress2);
	DDX_Text(pDX, IDC_EDIT_BILL_CITY, m_szBillCity);
	DDX_Text(pDX, IDC_EDIT_BILL_STATE, m_szBillState);
	DDX_Text(pDX, IDC_EDIT_BILL_ZIPCODE, m_szBillZipcode);
	DDX_Text(pDX, IDC_EDIT_BILL_COUNTRY, m_szBillCountry);
	DDX_Text(pDX, IDC_EDIT_PASSWORD, m_szUserPassword);
	DDX_Text(pDX, IDC_EDIT_SHIPTO, m_szShipTo);
	DDX_Text(pDX, IDC_EDIT_BILLTO, m_szBillTo);
	DDX_Text(pDX, IDC_EDIT_FEE_MAX, m_dFeeMax);
	DDX_Control(pDX, IDC_COMBO_COPYRIGHT, m_ctrlCopyright);
	DDX_Check(pDX, IDC_CHECK_TAXABLE, m_bTaxable);
	DDX_Check(pDX, IDC_CHECK_NOTIFY, m_bNotify);
}


BEGIN_MESSAGE_MAP(CDlgEditUser, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_PASSWORD, &CDlgEditUser::OnBnClickedButtonPassword)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, &CDlgEditUser::OnBnClickedButtonDelete)
	ON_BN_CLICKED(IDC_CHECK_ACTIVATED, &CDlgEditUser::OnBnClickedCheckActivated)
END_MESSAGE_MAP()


// CDlgEditUser message handlers

BOOL CDlgEditUser::OnInitDialog()
{
	CDialog::OnInitDialog();

	// get strings
	appConfig.m_SQL.GetDescriptions( CMySQLEx::user_type, &m_szaUserType );
	appConfig.m_SQL.GetDescriptions( CMySQLEx::referrals, &m_szaReferrals );
	appConfig.m_SQL.GetDescriptions( CMySQLEx::method, &m_szaMethod );
	appConfig.m_SQL.GetDescriptions( CMySQLEx::copyright, &m_szaCopyright );

	// set the random seed
	srand( (unsigned)time( NULL ) );

	// check if adding
	if( m_bAddUser )
	{
		// show password controls
		GetDlgItem( IDC_STATIC_PASSWORD )->ShowWindow( SW_SHOW );
		GetDlgItem( IDC_EDIT_PASSWORD )->ShowWindow( SW_SHOW );
		GetDlgItem( IDC_BUTTON_PASSWORD )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_BUTTON_DELETE )->ShowWindow( SW_HIDE );
		// set title
		SetWindowText( _T("Add user") );

		INT nSel = 0;
		CString szTemp;
		// set m_ctrlUserType
		for( INT nIndex = 0; nIndex < m_szaUserType.GetCount(); nIndex++ )
		{
			szTemp = m_szaUserType.GetAt( nIndex );
			if( !szTemp.IsEmpty() )
			{
				nSel = m_ctrlUserType.AddString( szTemp );
				m_ctrlUserType.SetItemData( nSel, nIndex );
			}
		}
		m_ctrlUserType.SetCurSel( 0 );

		// set m_ctrlReferrals
		for( INT nIndex = 0; nIndex < m_szaReferrals.GetCount(); nIndex++ )
		{
			szTemp = m_szaReferrals.GetAt( nIndex );
			if( !szTemp.IsEmpty() )
			{
				nSel = m_ctrlReferrals.AddString( szTemp );
				m_ctrlReferrals.SetItemData( nSel, nIndex );
			}
		}
		m_ctrlReferrals.SetCurSel( 0 );

		// set m_ctrlCopyright
		for( INT nIndex = 0; nIndex < m_szaCopyright.GetCount(); nIndex++ )
		{
			szTemp = m_szaCopyright.GetAt( nIndex );
			if( !szTemp.IsEmpty() )
			{
				nSel = m_ctrlCopyright.AddString( szTemp );
				m_ctrlCopyright.SetItemData( nSel, nIndex );
			}
		}
		m_ctrlCopyright.SetCurSel( 0 );

		// set m_ctrlMethod
		for( INT nIndex = 0; nIndex < m_szaMethod.GetCount(); nIndex++ )
		{
			szTemp = m_szaMethod.GetAt( nIndex );
			if( !szTemp.IsEmpty() )
			{
				nSel = m_ctrlMethod.AddString( szTemp );
				m_ctrlMethod.SetItemData( nSel, nIndex );
			}
		}
		m_ctrlMethod.SetCurSel( 0 );

		return TRUE;
	}
	else
	{
		// show password controls
		GetDlgItem( IDC_STATIC_PASSWORD )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_PASSWORD )->ShowWindow( SW_HIDE );
	}

	USER_SET User;

	// pick list
	if( m_nUserID < 0 )
	{
		CDlgPickUser dlgPickUser;
		if( dlgPickUser.DoModal() == IDOK )
		{
			// set user filter
			m_szUserEmail = dlgPickUser.m_szEmail;
		}
		else
		{
			EndDialog( IDCANCEL );
			return TRUE;
		}
		// get user set
		if( !appConfig.m_SQL.GetUserSet( &User, m_szUserEmail ))
		{
			AfxMessageBox( _T("ERROR getting user.") );
			EndDialog( IDCANCEL );
			return TRUE;
		}
	}
	else
	{
		// get user set
		if( !appConfig.m_SQL.GetUserSet( &User, m_nUserID ))
		{
			AfxMessageBox( _T("ERROR getting user.") );
			EndDialog( IDCANCEL );
			return TRUE;
		}
	}

	INT nCurSel = 0;
	INT nSel = 0;
	CString szTemp;

	// set user id
	m_nUserID = User.m_id;
	// set current
	m_szID.Format( _T("%.8ld"), User.m_id );
	m_szDateJoin = User.m_date_join.Format( appConfig.m_General.szDateFormat );
	m_szDateLogin = User.m_date_login.Format( appConfig.m_General.szDateFormat );
	m_szUserPassword = User.m_pwd;
	m_szUserName = User.m_name;
	m_szUserEmail = User.m_email;
	m_szCompany = User.m_company;
	m_szPhone = User.m_phone;
	m_szFax = User.m_fax;
	m_szShipTo = User.m_shipto;
	m_szAddress1 = User.m_address1;
	m_szAddress2 = User.m_address2;
	m_szCity = User.m_city;
	m_szState = User.m_state;
	m_szZipCode = User.m_zipcode;
	m_szCountry = User.m_country;
	m_szBillTo = User.m_billto;
	m_szBillAddress1 = User.m_billaddress1;
	m_szBillAddress2 = User.m_billaddress2;
	m_szBillCity = User.m_billcity;
	m_szBillState = User.m_billstate;
	m_szBillZipcode = User.m_billzipcode;
	m_szBillCountry = User.m_billcountry;
	m_szAltEmail1 = User.m_altemail1;
	m_bTaxable = (BOOL)User.m_taxable;
	m_dFeeMax = User.m_fee_max;
	m_nActivationCode = User.m_activation_code;
	m_szActivated = (BOOL)User.m_activated;
	m_bNotify = (BOOL)User.m_notify;
	m_szAccountNumber = User.m_account_number;
	m_szShippingNumber = User.m_shipping_number;
	m_szNotes = User.m_notes;

	// set m_ctrlUserType
	for( INT nIndex = 0; nIndex < m_szaUserType.GetCount(); nIndex++ )
	{
		szTemp = m_szaUserType.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlUserType.AddString( szTemp );
			m_ctrlUserType.SetItemData( nSel, nIndex );
			if( User.m_type == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlUserType.SetCurSel( nCurSel );

	// set m_ctrlReferrals
	for( INT nIndex = 0; nIndex < m_szaReferrals.GetCount(); nIndex++ )
	{
		szTemp = m_szaReferrals.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlReferrals.AddString( szTemp );
			m_ctrlReferrals.SetItemData( nSel, nIndex );
			if( User.m_referrals == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlReferrals.SetCurSel( nCurSel );

	// set m_ctrlCopyright
	for( INT nIndex = 0; nIndex < m_szaCopyright.GetCount(); nIndex++ )
	{
		szTemp = m_szaCopyright.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlCopyright.AddString( szTemp );
			m_ctrlCopyright.SetItemData( nSel, nIndex );
			if( User.m_copyright == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlCopyright.SetCurSel( nCurSel );

	// set m_ctrlMethod
	for( INT nIndex = 0; nIndex < m_szaMethod.GetCount(); nIndex++ )
	{
		szTemp = m_szaMethod.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlMethod.AddString( szTemp );
			m_ctrlMethod.SetItemData( nSel, nIndex );
			if( User.m_method_id == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlMethod.SetCurSel( nCurSel );

	// Disable Referrals
	if( appConfig.m_Options.bDisableReferrals )
	{
		GetDlgItem( IDC_STATIC_REFERRALS )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_COMBO_REFERRALS )->ShowWindow( SW_HIDE );		
	}
	// Disable Copyright
	if( appConfig.m_Options.bDisableCopyright )
	{
		GetDlgItem( IDC_STATIC_COPYRIGHT )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_COMBO_COPYRIGHT )->ShowWindow( SW_HIDE );
	}

	// disable activate for external since they should not be activated on internal
	if( User.m_external_id > 0 )
	{
		GetDlgItem( IDC_CHECK_ACTIVATED )->EnableWindow( FALSE );
	}

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgEditUser::OnOK()
{
	// update data
	UpdateData();

	if( m_szUserEmail.IsEmpty() )
	{
		AfxMessageBox( _T("Please enter at least a user email.") );
		return;
	}

	// check if adding
	if( m_bAddUser )
	{
		// check if exists
		LONG nCount = appConfig.m_SQL.GetRowCount( CMySQLEx::users, _T("name"), m_szUserName );
		if( nCount > 0 )
		{
			AfxMessageBox( _T("Name address already exists, please use a different email address.") );
			return;
		}
	}

	USER_SET User;

	// get current
	if( !appConfig.m_SQL.GetUserSet( &User, m_nUserID ))
	{
		AfxMessageBox( _T("ERROR getting user.") );
		return;
	}

	// change password
	if( m_bChangePwd || m_bAddUser )
	{
		// set password
		CString szHash;
		// convert for hash
		szHash = m_szUserPassword;
		appConfig.Hash( &szHash );
		// change password
		User.m_pwd = szHash;
		User.m_date_join = COleDateTime::GetCurrentTime();
	}
	else
	{
		// same password
		User.m_pwd = m_szUserPassword;
	}

	// set current
	User.m_level = 0;
	User.m_name = m_szUserName;
	User.m_email = m_szUserEmail;
	User.m_company = m_szCompany;
	User.m_phone = m_szPhone;
	User.m_fax = m_szFax;
	User.m_shipto = m_szShipTo;
	User.m_address1 = m_szAddress1;
	User.m_address2 = m_szAddress2;
	User.m_city = m_szCity;
	User.m_state = m_szState;
	User.m_zipcode = m_szZipCode;
	User.m_country = m_szCountry;
	User.m_billto = m_szBillTo;
	User.m_billaddress1 = m_szBillAddress1;
	User.m_billaddress2 = m_szBillAddress2;
	User.m_billcity = m_szBillCity;
	User.m_billstate = m_szBillState;
	User.m_billzipcode = m_szBillZipcode;
	User.m_billcountry = m_szBillCountry;
	User.m_altemail1 = m_szAltEmail1;
	User.m_library_id = 0;
	User.m_taxable = (INT)m_bTaxable;
	User.m_fee_max = m_dFeeMax;
	User.m_activation_code = m_nActivationCode;
	User.m_activated = (INT)m_szActivated;
	User.m_notify = (INT)m_bNotify;
	User.m_account_number = m_szAccountNumber;
	User.m_shipping_number = m_szShippingNumber;
	User.m_notes = m_szNotes;

	// get user type
	User.m_type = m_ctrlUserType.GetItemData( m_ctrlUserType.GetCurSel() );
	// get referrals
	User.m_referrals = m_ctrlReferrals.GetItemData( m_ctrlReferrals.GetCurSel() );
	// get copyright
	User.m_copyright = m_ctrlCopyright.GetItemData( m_ctrlCopyright.GetCurSel() );
	// get method id
	User.m_method_id = m_ctrlMethod.GetItemData( m_ctrlMethod.GetCurSel() );

	// save it
	appConfig.m_SQL.SetUserSet( &User, m_nUserID, m_bAddUser );

	// check if adding
	if( m_bAddUser )
	{
		// get user set
		if( !appConfig.m_SQL.GetUserSet( &User, m_szUserEmail ))
		{
			AfxMessageBox( _T("ERROR getting user.") );
			EndDialog( IDCANCEL );
			return;
		}
		// set user id
		m_nUserID = User.m_id;
	}

	// close dialog
	CDialog::OnOK();
}

void CDlgEditUser::OnBnClickedButtonPassword()
{
	CDlgChangePwd dlgPassword;
	// show dialog
	if( dlgPassword.DoModal() == IDOK )
	{
		m_bChangePwd = TRUE;
		m_szUserPassword = dlgPassword.m_szPassword;
		UpdateData( FALSE );
	}
}

void CDlgEditUser::OnBnClickedButtonDelete()
{
	// check to make sure
	CString szMessage;
	szMessage.Format( _T("Deleting user will also delete all user requests both pending and complete.\n\nAre you sure you want to delete %s (ID=%ld)?"), m_szUserEmail, m_nUserID );
	if( AfxMessageBox( szMessage ) == IDNO )
		return;

	CDlgConfirm dlgConfirm;
	dlgConfirm.m_szAction = _T("This will permantly delete this user and all requests.\n\nAre you sure you want to proceed?");
	if( dlgConfirm.DoModal() != IDOK )
		return;

	// remove row
	appConfig.m_SQL.RemoveRow( CMySQLEx::users, m_nUserID );

	// remove user requests
	appConfig.m_SQL.RemoveRows( CMySQLEx::requests, _T("user_id"), m_nUserID );

	// set new user Id
	m_nUserID = -1;

	USER_SET User;
	INT nCurSel = 0;
	INT nSel = 0;
	CString szTemp;

	// pick list
	CDlgPickUser dlgPickUser;
	if( dlgPickUser.DoModal() == IDOK )
	{
		// set user filter
		m_szUserEmail = dlgPickUser.m_szEmail;
	}
	else
	{
		EndDialog( IDCANCEL );
		return;
	}
	// get user set
	if( !appConfig.m_SQL.GetUserSet( &User, m_szUserEmail ))
	{
		AfxMessageBox( _T("ERROR getting user.") );
		EndDialog( IDCANCEL );
		return;
	}

	// set user id
	m_nUserID = User.m_id;
	// set current
	m_szID.Format( _T("%.8ld"), User.m_id );
	m_szDateJoin = User.m_date_join.Format( appConfig.m_General.szDateFormat );
	m_szDateLogin = User.m_date_login.Format( appConfig.m_General.szDateFormat );
	m_szUserPassword = User.m_pwd;
	m_szUserName = User.m_name;
	m_szUserEmail = User.m_email;
	m_szCompany = User.m_company;
	m_szShipTo = User.m_shipto;
	m_szAddress1 = User.m_address1;
	m_szAddress2 = User.m_address2;
	m_szCity = User.m_city;
	m_szState = User.m_state;
	m_szZipCode = User.m_zipcode;
	m_szCountry = User.m_country;
	m_szBillTo = User.m_billto;
	m_szBillAddress1 = User.m_billaddress1;
	m_szBillAddress2 = User.m_billaddress2;
	m_szBillCity = User.m_billcity;
	m_szBillState = User.m_billstate;
	m_szBillZipcode = User.m_billzipcode;
	m_szBillCountry = User.m_billcountry;
	m_szAltEmail1 = User.m_altemail1;
	m_szPhone = User.m_phone;
	m_szFax = User.m_fax;
	m_szActivated = (BOOL)User.m_activated;
	m_szAccountNumber = User.m_account_number;
	m_szShippingNumber = User.m_shipping_number;
	m_szNotes = User.m_notes;
	m_nActivationCode = User.m_activation_code;
	m_dFeeMax = User.m_fee_max;

	// set m_ctrlUserType
	for( INT nIndex = 0; nIndex < m_szaUserType.GetCount(); nIndex++ )
	{
		szTemp = m_szaUserType.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			if( User.m_type == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlUserType.SetCurSel( nCurSel );

	// set m_ctrlReferrals
	for( INT nIndex = 0; nIndex < m_szaReferrals.GetCount(); nIndex++ )
	{
		szTemp = m_szaReferrals.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			if( User.m_referrals == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlReferrals.SetCurSel( nCurSel );

	// set m_ctrlCopyright
	for( INT nIndex = 0; nIndex < m_szaCopyright.GetCount(); nIndex++ )
	{
		szTemp = m_szaCopyright.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			if( User.m_copyright == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlCopyright.SetCurSel( nCurSel );

	// set m_ctrlMethod
	for( INT nIndex = 0; nIndex < m_szaMethod.GetCount(); nIndex++ )
	{
		szTemp = m_szaMethod.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			if( User.m_method_id == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlMethod.SetCurSel( nCurSel );

	// update data
	UpdateData( FALSE );
}

void CDlgEditUser::OnBnClickedCheckActivated()
{
	// generate random activation code	rand(1000,9999);
	m_nActivationCode = rand()%10000;
}
